package com.sendinfo.sso;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sendinfo.common.lang.TxtUtil;
import com.sendinfo.sso.HttpUtil.SessionKey;

public class AuthHelperImpl extends HelperBase implements AuthHelper {
	private String domain;
	private boolean sso;
	@Override
	public void putCheckCode(String code, HttpServletRequest req) {
		if (null != code) HttpUtil.put(req.getSession(), SessionKey.LOGINING_CHECK_CODE, code.trim());
		else HttpUtil.remove(req.getSession(), SessionKey.LOGINING_CHECK_CODE);
	}

	@Override
	public boolean checkCode(String code, HttpServletRequest req) {
		if (TxtUtil.isEmpty(code)) return false;
		return code.trim().equals(HttpUtil.get(req.getSession(), SessionKey.LOGINING_CHECK_CODE));
	}

	@Override
	public Object checkUserId(HttpServletRequest req) {
		return getUserId(req);
	}

	@Override
	public void putUserId(Object userId,  HttpServletRequest req, HttpServletResponse resp) {
		//add session
		HttpUtil.put(req.getSession(), SessionKey.LOGINED_USER, userId);
	}

	@Override
	public void removeUserId(HttpServletRequest req, HttpServletResponse resp) {
		SessionKey keys[] = {SessionKey.LOGINED_USER};
		HttpUtil.remove(resp, keys);
	}

	@Override
	public String getRedirect(HttpServletRequest req) {
		return (String) HttpUtil.get(req.getSession(), SessionKey.LOGINING_REDIRECT);
	}

	@Override
	public void putRedirect(String redirect, HttpServletRequest req) {
		if (null == redirect) HttpUtil.remove(req.getSession(), SessionKey.LOGINING_REDIRECT);
		else HttpUtil.put(req.getSession(), SessionKey.LOGINING_REDIRECT, redirect);
	}

	protected Object getUserId(HttpServletRequest req) {
		Object userID =  HttpUtil.get(req.getSession(), SessionKey.LOGINED_USER);
		return userID;
	}

	@Override
	public String getAreaCode(HttpServletRequest req) {
		return HttpUtil.get(req, SessionKey.EXPLORER_IPAREA);
	}

	@Override
	public void putAreaCode(HttpServletRequest req,String areaCode) {
		HttpUtil.put(req.getSession(), SessionKey.EXPLORER_IPAREA, areaCode);
		
	}

	@Override
	public String getDomain() {
		return domain;
	}

	@Override
	public boolean isSSO() {
		return sso;
	}
	public void setDomain(String domain){
		this.domain=domain;
	}
	public void setSSO(boolean sso){
		this.sso=sso;
	}

	@Override
	public Object checkLogin(HttpServletRequest req) {
		// TODO Auto-generated method stub
		return null;
	}
}
